import axios from 'axios';

// 登录请求方法
const loginreq = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        console.log('请求前格式化参数',headers,data,ret);
        return ret
      }
    ]
  }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json',
      token: localStorage.getItem('logintoken')
    },
    params: params,
    data: params,
    traditional: true,
    transformRequest: [
      function(data,headers){
        console.log('this ',this)
        return JSON.stringify(data);
      }
      // function (data,headers) {
      //   let ret = ''
      //   for (let it in data) {
      //     ret +=
      //       encodeURIComponent(it) +
      //       '=' +
      //       encodeURIComponent(data[it]) +
      //       '&'
      //   }
      //   console.log('请求前格式化参数2',headers,data,ret);
      //   return ret
      // }
    ]
  }).then(res => res.data);
};

export {
  loginreq,
  req
}
